<!DOCTYPE html>
<html>
<head>
  <title>PushRecver.java</title>
  <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
  <link rel='stylesheet' type='text/css' href='../../../../../../coverage.css'/>
  <link rel='shortcut icon' type='image/png' href='../../../../../../logo.png'/>
  <script type='text/javascript' src='../../../../../../coverage.js'></script>
  <script type='text/javascript' src='../../../../../../prettify.js'></script>
</head>
<body onload='prettyPrint()'>
  <table cellpadding='0' cellspacing='1'>
    <caption>igraph_client_java/src/main/java/com/taobao/igraph/vipserver/client/core/PushRecver.java</caption>
    <tr>
      <td class='line'>1</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>package com.taobao.igraph.vipserver.client.core;</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'></td><td>&nbsp;</td>
      <td><pre class='imports prettyprint' onclick='showHideLines(this)'><div>import java.net.DatagramPacket;
import java.net.DatagramSocket;

import org.apache.commons.lang.StringEscapeUtils;

import com.taobao.igraph.vipserver.client.utils.IOUtils;
import com.taobao.igraph.vipserver.client.utils.UtilAndComs;
</div><span>import ...</span></pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'></td><td>&nbsp;</td>
      <td class='comment' onclick='showHideLines(this)'><div>/**
 * Innovated By: Xuanyin.zy@taobao.com
 * &lt;p/>
 * 2014-15-04 10:20
 */</div><span>/*...*/</span></td>
    </tr>
    <tr>
      <td class='line'>17</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>public class PushRecver implements Runnable {</pre></td>
    </tr>
    <tr>
      <td class='line'>18</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    public static int UDP_MSS = 64 * 1024;</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>20</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    private DatagramSocket udpSocket;</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>22</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    private Thread pushRcev;</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>24</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    public PushRecver() {</pre></td>
    </tr>
    <tr>
      <td class='line'>25</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        try {</pre></td>
    </tr>
    <tr>
      <td class='line'>26</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            udpSocket = new DatagramSocket();</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>28</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            pushRcev = new Thread(this, "com.taobao.vipserver.pushrecv");</pre></td>
    </tr>
    <tr>
      <td class='line'>29</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            pushRcev.setDaemon(true);</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>31</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            pushRcev.start();</pre></td>
    </tr>
    <tr>
      <td class='line'>32</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        } catch (Exception e) {</pre></td>
    </tr>
    <tr>
      <td class='line'>33</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            VIPClient.LOG.fatal("init udp socket failed", e);</pre></td>
    </tr>
    <tr>
      <td class='line'>34</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        }</pre></td>
    </tr>
    <tr>
      <td class='line'>35</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>37</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    @Override</pre></td>
    </tr>
    <tr>
      <td class='line'>38</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    public void run() {</pre></td>
    </tr>
    <tr>
      <td class='line'>39</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        while (true) {</pre></td>
    </tr>
    <tr>
      <td class='line'>40</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            try {</pre></td>
    </tr>
    <tr>
      <td class='line'>41</td><td>&nbsp;</td>
      <td><pre class='comment'>                // byte[] is initialized with 0 full filled by default</pre></td>
    </tr>
    <tr>
      <td class='line'>42</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                byte[] buffer = new byte[UDP_MSS];</pre></td>
    </tr>
    <tr>
      <td class='line'>43</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                DatagramPacket packet = new DatagramPacket(buffer, buffer.length);</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>45</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                udpSocket.receive(packet);</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>47</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                String json = new String(IOUtils.tryDecompress(packet.getData()), "GBK").trim();</pre></td>
    </tr>
    <tr>
      <td class='line'>48</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                VIPClient.LOG.info("received push data: " + json + " from " + packet.getAddress().toString());</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>50</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                PushPacket pushPacket = UtilAndComs.GSON.fromJson(json, PushPacket.class);</pre></td>
    </tr>
    <tr>
      <td class='line'>51</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                String ack;</pre></td>
    </tr>
    <tr>
      <td class='line'>52</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                if (pushPacket.type.equals("dom")) {</pre></td>
    </tr>
    <tr>
      <td class='line'>53</td><td>&nbsp;</td>
      <td><pre class='comment'>                    // dom update</pre></td>
    </tr>
    <tr>
      <td class='line'>54</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                    HostReactor.processDomJSON(pushPacket.data);</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>56</td><td>&nbsp;</td>
      <td><pre class='comment'>                    // send ack to server</pre></td>
    </tr>
    <tr>
      <td class='line'>57</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                    ack = "{\"type\": \"push-ack\""</pre></td>
    </tr>
    <tr>
      <td class='line'>58</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                            + ", \"lastRefTime\":\"" + pushPacket.lastRefTime</pre></td>
    </tr>
    <tr>
      <td class='line'>59</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                            + "\", \"data\":" + "\"\"}";</pre></td>
    </tr>
    <tr>
      <td class='line'>60</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                } else if (pushPacket.type.equals("dump")) {</pre></td>
    </tr>
    <tr>
      <td class='line'>61</td><td>&nbsp;</td>
      <td><pre class='comment'>                    // dump data to server</pre></td>
    </tr>
    <tr>
      <td class='line'>62</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                    ack = "{\"type\": \"dump-ack\""</pre></td>
    </tr>
    <tr>
      <td class='line'>63</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                            + ", \"lastRefTime\": \"" + pushPacket.lastRefTime</pre></td>
    </tr>
    <tr>
      <td class='line'>64</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                            + "\", \"data\":" + "\""</pre></td>
    </tr>
    <tr>
      <td class='line'>65</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                            + StringEscapeUtils.escapeJavaScript(UtilAndComs.GSON.toJson(HostReactor.getDomMap()))</pre></td>
    </tr>
    <tr>
      <td class='line'>66</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                            + "\"}";</pre></td>
    </tr>
    <tr>
      <td class='line'>67</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                } else {</pre></td>
    </tr>
    <tr>
      <td class='line'>68</td><td>&nbsp;</td>
      <td><pre class='comment'>                    // do nothing send ack only</pre></td>
    </tr>
    <tr>
      <td class='line'>69</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                    ack = "{\"type\": \"unknown-ack\""</pre></td>
    </tr>
    <tr>
      <td class='line'>70</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                            + ", \"lastRefTime\":\"" + pushPacket.lastRefTime</pre></td>
    </tr>
    <tr>
      <td class='line'>71</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                            + "\", \"data\":" + "\"\"}";</pre></td>
    </tr>
    <tr>
      <td class='line'>72</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>74</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                udpSocket.send(new DatagramPacket(ack.getBytes(),ack.getBytes().length, packet.getSocketAddress()));</pre></td>
    </tr>
    <tr>
      <td class='line'>75</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            } catch (Exception e) {</pre></td>
    </tr>
    <tr>
      <td class='line'>76</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>                VIPClient.LOG.error("error while receiving push data", e);</pre></td>
    </tr>
    <tr>
      <td class='line'>77</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>            }</pre></td>
    </tr>
    <tr>
      <td class='line'>78</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        }</pre></td>
    </tr>
    <tr>
      <td class='line'>79</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>81</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    public static class PushPacket {</pre></td>
    </tr>
    <tr>
      <td class='line'>82</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        public String type;</pre></td>
    </tr>
    <tr>
      <td class='line'>83</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        public long lastRefTime;</pre></td>
    </tr>
    <tr>
      <td class='line'>84</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        public String data;</pre></td>
    </tr>
    <tr>
      <td class='line'>85</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    }</pre></td>
    </tr>
    <tr><td class='line'></td><td colspan='2'>&nbsp;</td></tr>
    <tr>
      <td class='line'>87</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    public int getUDPPort() {</pre></td>
    </tr>
    <tr>
      <td class='line'>88</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>        return udpSocket.getLocalPort();</pre></td>
    </tr>
    <tr>
      <td class='line'>89</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>    }</pre></td>
    </tr>
    <tr>
      <td class='line'>90</td><td>&nbsp;</td>
      <td><pre class='prettyprint'>}</pre></td>
    </tr>
  </table>
</body>
</html>
